package com.gxd.dao.impl;

import com.gxd.dao.OrderDao;
import com.gxd.domain.Address;
import com.gxd.domain.Order;
import com.gxd.domain.OrderDetail;
import com.gxd.util.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public Order selectByAid(int id) {
        QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
        String sql=" select id,uid,money,status,time,aid from tb_order where aid=?  ";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Order.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insertOrder(Order order) {
        QueryRunner queryRunner = new QueryRunner();
        Connection connection = DBUtils.getConnection();
        String sql=" insert into tb_order values(?,?,?,?,?,?) ";
        Object[] params={ order.getId(),order.getUid(),order.getMoney(),order.getStatus(),order.getTime(),order.getAid()};
        try {
            queryRunner.update(connection,sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void paySuccess(Order order) {
        QueryRunner queryRunner = new QueryRunner();
        Connection connection = DBUtils.getConnection();
        String sql=" update tb_order set status=? where id=? ";

        try {
            queryRunner.update(connection,sql,order.getStatus(),order.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Order> findAll() {
        QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());

        String sql=" select id,uid,money,status,time,aid from tb_order ";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Order.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
